<template>
      <div id="dia">
            <van-dialog
            v-model="DialogData.show"
            title="立即咨询"
            show-cancel-button
            @confirm="subForm"
            >
                        <div class="form">
                              
                              <van-cell-group>
                                    <van-field
                                    v-model="formData.username"
                                    required
                                    clearable
                                    label="姓名"
                                    
                                    placeholder="请输入姓名"
                              
                                    />

                                    <van-field
                                    v-model="formData.telephone"
                                   
                                    label="手机"
                                    placeholder="请输入手机"
                                    required
                                    />
                                    </van-cell-group>
                        </div>
            </van-dialog>
      </div>
</template>

<script>
      export default {
            props: {
                  DialogData: Object
            },
            data() {
                  return {
                        formData: {
                              username: "",
                              telephone: ""
                        }
                  }
            },
            methods: {
                  async subForm() {
                        let reg = /^1[356789]\d{9}$/gi
                        let { username, telephone } = this.formData
                        this.formData.key = this.$key
                        let isMatch = reg.test(telephone)
                        if (!username) {
                             this.$toast.fail("请输入姓名!") 
                             return false
                        } else if (!telephone) {
                              this.$toast.fail("请输入手机号!") 
                             return false
                        } else if (telephone && !isMatch) {
                               this.$toast.fail("手机号格式错误!") 
                               this.formData.telephone = ""
                             return false
                        } else {
                              let res = await this.$axios.post("/index/index/leave", this.formData)
                              //console.log(res);
                              let code = res.data.code
                              if (code ==202) {
                                    this.$toast.success("提交成功!")
                                    this.formData = {}
                              } else {
                                   this.$toast.fail("网络繁忙!")
                                    this.formData = {} 
                              }
                        }
                  } 
            },
      }
</script>

<style lang="scss" scoped>
.form {
      margin-top: 0.3rem;
}
.van-dialog {
      color:#000
}
</style>